<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class ProductController extends BaseController {

    protected $search_cate_key = 'search_cate';
    
    
    public function search($keyword=null) {
        $this->assign("keyword", $keyword);
        
        $N = M("Product");
        $C = M("Category");
        $cid=I('get.cid');
        $pid=I('get.pid');
        if(empty($keyword) && empty($cid)){
            $this->assign("head_class", "");
        }

        $where = "n.status='1' AND n.lang='".LANG_SET."' ";
        if($cid){
            $c_name=["n.t_xq","n.t_hy","n.t_sj","n.t_jx","n.t_gj","n.t_lx"];
            if(($pid==2 )){
                $cur_map["pid"] = $cid;
                $cids = $C->where($cur_map)->select();
                $cur_index = 0;
               foreach ($cids as $c_item) {
                    if(!$cur_index){
                        $where.=" AND ".$c_name[$pid-1]."=".$c_item["cid"];
                        $cur_index++;
                    }else{
                       $where.=" OR ".$c_name[$pid-1]."=".$c_item["cid"];
                    }
                }
            }else{
                $where.=" AND ".$c_name[$pid-1]."='$cid'";
            }
        }
        if(!empty($keyword)){
            $where.=" AND (n.title LIKE '%$keyword%' OR n.man  LIKE '%$keyword%'  OR n.brand LIKE '%$keyword%')";
        }


         $Fromyear = $_GET['Fromyear']; 
         $Toyear = $_GET['Toyear']; 

        if(!empty($Fromyear)&&!empty($Toyear)){
              $c_name=["n.t_xq","n.t_hy","n.t_sj","n.t_jx","n.t_gj","n.t_lx"];
           $count=array();
           $year_index = 0;
           for($x=$Fromyear; $x<=$Toyear; $x++){
               $y=$x."年";
               $condition1["name"] = $y;
               $str1=$C->where($condition1)->find(); 
             if(!$year_index){
                 $where.=" AND ".$c_name[2]."=".$str1['cid'];
             }else{
                 $where.=" OR ".$c_name[2]."=".$str1['cid'];
             }
              $year_index++;
           }
        }
       
      

        $count = $N->table($N->getTableName().' n')
            ->field('n.id')
            ->where($where)->count();
        $page = new \Think\Page($count,C('LISTNUM.prolist'));

        $showPage = $page->show();
        $this->assign("page", $showPage);
        $list=$N->table($N->getTableName().' n')
            ->where($where)->order('id desc')->limit("$page->firstRow, $page->listRows")->select();
        $this->assign("list", $list);
        
        
        //*******  EK熊 添加 ********
        R('Public/category');
        $this->setSearchCate($cid);
        
        //*******  end ********
        
        
        $this->assign('webtitle',L('T_HOME'));
        $this->display();
    }
    
    
    /**
     * 记录显示 搜索分类
     * @param unknown $cid
     * date:2016年12月21日
     * author: EK_熊
     */
    public function setSearchCate($cid=''){
        
        $searchCate = session($this->search_cate_key);
        if (!array_key_exists($cid,$searchCate) && $cid) {
            $cateName = M('Category')->where(['cid'=>$cid])->getField('name');
            
            $searchCate[$cid] = $cateName;
           
            session($this->search_cate_key,$searchCate);
        }
        $this->assign('search_cate',$searchCate);
    }
    
    /**
     * 清除单个 分类条件
     * @param string $cid
     * date:2016年12月21日
     * author: EK_熊
     */
    public function deleteSearchCate($cid=''){
        $cate = session($this->search_cate_key);
        unset($cate[$cid]);
        session($this->search_cate_key,$cate);
        $this->redirect('search');
    }
    
    
    

    /**
     * 详情页
     */
    public function details(){
        $id=I('get.id');

        // $id=I('get.id');
        $m_product=M('product');
        $C = M("Category");

        if(!$id){$this->_empty($id);}
        $map['id']=$id;        
        if($info=$m_product->where($map)->find()){
            if($info['status']==0){
                $this->redirect('index/index');
            }

            $xq = $info['t_xq'];
            $map30['cid']=$xq;
            $crush30=$C->where($map30)->find();
            $crush30['name']=$C->where($map30)->getField('name');
            $crush30['cid']=$C->where($map30)->getField('cid');
             $this->assign('crush30',$crush30);
            // var_dump($crush30['cid']);

            $map20['cid']=$crush30['pid'];
            $crush20=$C->where($map20)->find();
            $crush20['name']=$C->where($map20)->getField('name');
            $this->assign('crush20',$crush20);
             // var_dump($crush20);

            $map10['cid']=$crush20['pid'];
            $crush10=$C->where($map10)->find();
            $crush10['name']=$C->where($map10)->getField('name');
            $this->assign('crush10',$crush10);
             // var_dump($crush10);
             
            $hy = $info['t_hy'];
            $map21['cid']=$hy;
            $crush21=$C->where($map21)->find();
            $crush21['name']=$C->where($map21)->getField('name');
            $crush21['cid']=$C->where($map21)->getField('cid');
            $this->assign('crush21',$crush21);
                // var_dump($crush21);

            $map11['cid']=$crush21['pid'];
            $crush11=$C->where($map11)->find();
            $crush11['name']=$C->where($map11)->getField('name');
            $crush11['cid']=$C->where($map11)->getField('cid');
            $this->assign('crush11',$crush11);
            // var_dump($crush11);
           
            $info['cname']=$C->where($map2)->getField('name');
            $this->assign('info',$info);
            $this->assign('images',get_img_array($info['image_id']));
            $this->assign('videos',@explode(',',$info['media_id']));
           

            $m_product->where($map)->setInc('click',1);
            $map['type']='n';
            $cat = new \Org\Util\Category('Category', array('cid', 'pid', 'name', 'fullname'),$map);

            // var_dump($info);

            $map5['cid']=$info['cid'];
            $map5['littleneed']=$info['littleneed'];
            $map5['need']=$info['need'];
            $map5['keywords']=$info['keywords'];
            $map5['status']=1;
            $map5['id']=array('neq',$map['id']);

            $count_keywords=$m_product->where($map5)->count();
            if ($count_keywords>=9) {
                $info2=$m_product->where($map5)->order('published desc')->limit(9)->select();
            }
            else{
                unset($map5['keywords']);
                $count_littleneed=$m_product->where($map5)->count();
                if ($count_littleneed>=9) {
                    $info2=$m_product->where($map5)->order('published desc')->limit(9)->select();
                }
                else{
                    unset($map5['littleneed']);
                    $count_need=$m_product->where($map5)->count();
                    if ($count_need>=9) {
                    $info2=$m_product->where($map5)->order('published desc')->limit(9)->select();
                    }
                    else{
                        unset($map5['need']);
                        $count_need=$m_product->where($map5)->count();
                        if ($count_need>=9) {
                        $info2=$m_product->where($map5)->order('published desc')->limit(9)->select();
                        }
                        else{
                            unset($map5['cid']);
                            $info2=$m_product->where($map5)->order('published desc')->limit(9)->select();
                        }
                    }
                }
            }
            $this->assign("info2", $info2);
            $where = "n.status='1' AND n.lang='".LANG_SET."' AND n.t_lx='$info[t_lx]' AND id!='$info[id]' ";
            $list=$m_product->table($m_product->getTableName().' n')
            // // ->join($C->getTableName().' c1 on c1.cid=n.t_xq')
            // // ->join($C->getTableName().' c2 on c2.cid=n.t_hy')
            // // ->join($C->getTableName().' c3 on c3.cid=n.t_gj')
            // // ->join($C->getTableName().' c4 on c4.cid=n.t_sj')
            // // ->join($C->getTableName().' c5 on c5.cid=n.t_jx')
            // // ->join($C->getTableName().' c6 on c6.cid=n.t_lx')
            // ->field('n.brand,n.id,n.t_xq,n.title,n.summary,n.update_time,n.image_id,c1.name as name_c1,c2.name as name_c2,c3.name as name_c3,c3.name as name_c3,c4.name as name_c4,c5.name as name_c5,c6.name as name_c6,n.published')
            ->where($where)->order('RAND()')->limit("1, 3")->select();

            $this->assign("list", $list);
            $this->assign('webtitle',$info['title'].'-'.L('T_PRODUCT'));
            $this->display();
        }else{
            $this->redirect('product/index');
        }
    }

}